package cc.cary.warehouse.config;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.configuration.ClassicConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * flyway配置
 */
@Configuration
public class FlywayConfig {

    @Value("${spring.datasource.url}")
    private String datasourceUrl;
    @Value("${spring.datasource.username}")
    private String databaseUsername;
    @Value("${spring.datasource.password}")
    private String databasePassword;

    @Bean
    Flyway flyway() {
        ClassicConfiguration classicConfiguration = new ClassicConfiguration();
        classicConfiguration.setDataSource(datasourceUrl, databaseUsername, databasePassword);
        classicConfiguration.setBaselineOnMigrate(true);
        classicConfiguration.setLocations(new Location("db/migration"));
        classicConfiguration.setPlaceholderReplacement(false); // 禁止类似${XXX}的变量替换
        Flyway flyway = new Flyway(classicConfiguration);
        flyway.repair();
        flyway.migrate();
        return flyway;
    }

}
